package org.weso.lodin.datasource.config;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.weso.lodin.dao.PrimaryObservationDAOImpl;
import org.weso.lodin.exceptions.LodinModelException;
import org.weso.lodin.generated.Dataset;
import org.weso.lodin.generated.IdIndicator;
import org.weso.lodin.generated.TypeTable;
import org.weso.lodin.generated.TypeValues;
import org.weso.lodin.utils.POIUtils;

public class RankingScoresDatasetCreator {
	private HSSFWorkbook wb;

	protected static Logger logger = Logger.getLogger(RankingScoresDatasetCreator.class);

	public RankingScoresDatasetCreator(InputStream data){
		try {			
			this.wb = new HSSFWorkbook(data);
		} catch (IOException e) {
			throw new LodinModelException(e, "Loading workbook");
		}
	}

	public List<Dataset> createIndicatorRankingsDatasets(String name) {
		try {
			return this.createIndicatorRankingDatasets(wb, name);
		} catch (DatatypeConfigurationException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return new LinkedList<Dataset>();
	}

	private List<Dataset> createIndicatorRankingDatasets(HSSFWorkbook wb, String name) throws DatatypeConfigurationException {
		List<Dataset> datasets = new LinkedList<Dataset>();
		HSSFSheet sheet = wb.getSheet(name);
		HSSFRow row = sheet.getRow(0);
		int physicalNumberOfCells = row.getPhysicalNumberOfCells();
		for(int i = 1; i<physicalNumberOfCells-1;i++){
			String indicator = POIUtils.extractValue(row.getCell(i)).split("-")[0].trim();
			String weightValue = POIUtils.extractValue(sheet.getRow(4).getCell(i));
			logger.info(name+", "+indicator+ ", "+ weightValue+", "+i);	
			datasets.add(createDataset(indicator, name, name, ""+i, "6", "66"));
			datasets.add(createDataset(indicator, name, name, ""+i, "69", "129"));
			datasets.add(createDataset(indicator, name, name, ""+i, "133", "93"));
			//To extract value the column is interpreted c[i]
		}
		return datasets;
	}

	public List<Dataset> createMeanSubindexRankingsDatasets(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.READINESS.value(),
				IdIndicator.THE_WEB.value(),
				IdIndicator.IMPACT.value()};
		String [] valueCols = {"B","C","D"};
		for(int i=0; i<valueCols.length;i++) {
			String idValue = String.valueOf(year);
			datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "263", "323"));
			//To extract value the column is interpreted c[encode[i]]
		}
		return datasets;
	}
	
	public List<Dataset> createMeanComponentDatasets(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.COMMUNICATIONS_INFRASTRUCTURE.value(),
				IdIndicator.INSTITUTIONAL_INFRASTRUCTURE.value(),
				IdIndicator.WEB_USAGE.value(),
				IdIndicator.WEB_CONTENT.value(),
				IdIndicator.SOCIAL.value(),
				IdIndicator.ECONOMIC.value(),
				IdIndicator.POLITICAL.value()
		}
				;
		String [] valueCols = {"B","C","D","E","F","G","H"};
		for(int i=0; i<valueCols.length;i++) {
			String idValue = String.valueOf(year);
			datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "198", "258"));
			//To extract value the column is interpreted c[encode[i]]
		}
		return datasets;
	}
	
	public List<Dataset> createGeoComponentDatasets(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.COMMUNICATIONS_INFRASTRUCTURE.value(),
				IdIndicator.INSTITUTIONAL_INFRASTRUCTURE.value(),
				IdIndicator.WEB_USAGE.value(),
				IdIndicator.WEB_CONTENT.value(),
				IdIndicator.SOCIAL.value(),
				IdIndicator.ECONOMIC.value(),
				IdIndicator.POLITICAL.value()
		};
		String [] valueCols = {"K","L","M","N","O","P","Q"};
		for(int i=0; i<valueCols.length;i++) {
			String idValue = String.valueOf(year);
			datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "198", "258"));
			//To extract value the column is interpreted c[encode[i]]
		}
		return datasets;
	}

	public List<Dataset> createGeoSubindexRankingDatasets(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.READINESS.value(),
				IdIndicator.THE_WEB.value(),
				IdIndicator.IMPACT.value()};
		String [] valueCols = {"H","I","J"};
			for(int i=0; i<valueCols.length;i++) {
				String idValue = String.valueOf(year);
				datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "263", "323"));
				//To extract value the column is interpreted c[encode[i]]
			}
		return datasets;
	}


	public List<Dataset> create1_100SubindexRankingDatasets(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.READINESS.value(),
				IdIndicator.THE_WEB.value(),
				IdIndicator.IMPACT.value()};
		String [] valueCols = {"B","C","D"};
			for(int i=0; i<valueCols.length;i++) {
				String idValue = String.valueOf(year);
				datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "336", "396"));
				//To extract value the column is interpreted c[encode[i]]
			}
		return datasets;
	}


	public List<Dataset> create1_100SubindexRankingDatasets_2(int year) throws DatatypeConfigurationException{
		List<Dataset> datasets = new LinkedList<Dataset>();
		String [] references = {
				IdIndicator.READINESS.value(),
				IdIndicator.THE_WEB.value(),
				IdIndicator.IMPACT.value()};
		String [] valueCols = {"H","I","J"};
		for(int i=0; i<valueCols.length;i++) {
				String idValue = String.valueOf(year);
				datasets.add(createDataset(references[i], idValue, idValue, valueCols[i], "336", "396"));
				//To extract value the column is interpreted c[encode[i]]
			}
		return datasets;
	}


	public static Dataset createDataset (
			String indicator, 
			String id, 
			String year, 
			String col, 
			String start, 
			String end) throws DatatypeConfigurationException{
		Dataset currentDataset = new Dataset();	
		currentDataset.setIndicator(IdIndicator.fromValue(indicator));		
		currentDataset.setId(id);//sheet-name
		TypeTable typeTable = new TypeTable();	
		typeTable.setRowStart(new BigInteger(start)); //constant
		typeTable.setRowEnd(new BigInteger(end));//constant
		TypeValues values = new TypeValues();
		values.setCountryCol("A"); //constant
		values.setValueCol(col);
		values.setYear(DatatypeFactory.newInstance().newXMLGregorianCalendar(year));//constant
		values.setDatatype("number");
		typeTable.getValues().add(values);
		currentDataset.getTable().add(typeTable );
		return currentDataset;
	}

}
